﻿using MediatR;
using SuYuanSystem.Applications;
using SuYuanSystem.Domain;
using SuYuanSystem.Repositories;
using SuYuanSystem.WebApi.MedialRs.Commands.TracebilityCommand;

namespace SuYuanSystem.WebApi.MedialRs.CommandHandlers.Tracebilitys
{
    public class TracebilityDeleteCommandHandler : IRequestHandler<TracebilityDeleteCommand, ApiResult<object>>
    {
        public TracebilityDeleteCommandHandler(IRepository<Tracebility> repository)
        {
            Repository = repository;
        }

        public IRepository<Tracebility> Repository { get; }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<object>> Handle(TracebilityDeleteCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<object> result = new ApiResult<object>();
                result.statusCode = StatusCode.Success;
                result.Message = "删除成功";

                var list = Repository.QueryFirstAsync(x => x.Id == request.Id).Result;
                if (list == null)
                {
                    result.statusCode = StatusCode.FaieId;
                    result.Message = "删除失败";
                    return Task.FromResult(result);
                }
                else
                {
                    list.IsDelete = true;
                    Repository.Update(list);
                    return Task.FromResult(result);
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
